#!/usr/bin/perl
#
# Create a GNUPLOT command file from the "starttime" section
# of the persons.count file.
#

use English;
use Reportsub;

$DEBUG = 0;

$usage = "Usage: $0 persons.count\n";
%monthnum = (
jan => 1,
feb => 2,
mar => 3,
apr => 4,
may => 5,
jun => 6,
jul => 7,
aug => 8,
sep => 9,
oct => 10,
nov => 11,
dec => 12);

while ($ARGV[0] =~ /^-/) {
    $opt = shift;
    if ($opt eq "-help") {
	die $usage;
    } else {
	die $usage;
    }
}
# Ignore records later than today
@date = gmtime();
$lastday = sprintf("%04d-%02d-%02d", $date[5]+1900, $date[4]+1, $date[3]);
warn "Ignoring stuff after $lastday\n";
while (<>) {
    if (/^\/\/.*:started$/.../^\/\//) {
	$DEBUG && warn $_;
	chop;
	if (/^"(\S{3}) (\d+)":(\d+)/) {
	    $month = $monthnum{$1};
	    if ($month == 0) {
		$illmonth += $3;
		next;
	    }
	    $year = $2;
	    $year += 1900 if $year < 100;
	    $date = sprintf("%4d-%02d-01", $year, $month);
	    do { $late += $3; next } if $date gt $lastday; # reject after today
	    do { $early += $3; next } if $date lt "1991-09-01"; # reject before Linux 0.01
	    $DEBUG && warn "Countign $3 at $date\n";
	    $count{$date} += $3;
	}
    }
}
warn "$illmonth records skipped because month is bad\n";
warn "$late records skipped because they are too new\n";
warn "$early records skipped because they are too old\n";


# The output of this can be merged with the "country.story" file format
$sum = 0;
for $date (sort(keys(%count))) {
    $sum += $count{$date};
    print "$date:Started:$sum\n";
}
warn "$sum total records\n";
